package com.example.temp.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.temp.entity.AssetInformation;
import com.example.temp.entity.AssetLend;
import com.example.temp.mapper.AssetInformationMapper;
import com.example.temp.mapper.AssetLendMapper;
import com.example.temp.utils.RandomNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AssetLendService extends ServiceImpl<AssetLendMapper, AssetLend> {

    @Autowired
    private AssetLendMapper assetLendMapper;

    @Autowired
    private AssetInformationService assetInformationService;

    /**
     * 保存添加的资产借出信息
     * 同意审核后才添加
     *
     * @param assetLend
     * @return
     */
    public boolean saveAssetLend(AssetLend assetLend) {
        assetLend.setLendSerialnumber(generateUniqueSerialNumber());
        assetLend.setAudit("否");
        return saveOrUpdate(assetLend);
    }


    /**
     * 审核：同意借出，
     *
     * @param assetLend
     * @return
     */
    public boolean auditLend(AssetLend assetLend) {//同意借出
        //不存在true,存在false
        AssetInformation asset = assetInformationService.QuerryAssetNumber(assetLend.getAssetNumber());
        if (asset != null) {//资产存在，修改其状态为借出
            if (asset.getAssetState() != "借出") {
                asset.setAssetState("借出");
                return assetInformationService.saveOrUpdate(asset);
            }
        }
        return false;
    }


    /**
     * 查询借出流水号是否存在
     * @param lendSerialNum
     * @return
     */
    public AssetLend QuerryLendSerialNum(String lendSerialNum){
        QueryWrapper<AssetLend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lend_serialNumber", lendSerialNum);
        return assetLendMapper.selectOne(queryWrapper);
    }
    /**
     * 生成借出编号
     *
     * @return
     */
    private String generateUniqueSerialNumber() {
        String serialNumber;
        boolean isUnique = false;

        Pattern pattern = Pattern.compile("\\d{14}");
        Matcher matcher;

        do {
            // 生成一个14位的随机数
            serialNumber = RandomNumberUtil.generateRandomNumber();
            // 匹配正则表达式
            matcher = pattern.matcher(serialNumber);

            // 检查数据库是否存在相同的资产编号
            QueryWrapper<AssetLend> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("lend_serialNumber", serialNumber);
            Long count = assetLendMapper.selectCount(queryWrapper);

            // 如果数据库中没有相同的资产编号，流水号是唯一的
            if (matcher.matches() && count == 0) {
                isUnique = true;
            }
        } while (!isUnique);

        return serialNumber;
    }

    /**
     * 计入还入条数
     * @return
     */
    public int CountLend(){
        int count=0;
        QueryWrapper<AssetLend> queryWrapper = new QueryWrapper<>();
        count= Math.toIntExact(assetLendMapper.selectCount(queryWrapper));
        return count;
    }





}
